/*
 * 函数: ajax
 * 参数: options
 * 返回值: undefined
 */
const ajax = options => {
  let params = {
      type: "get",
      url: "",
      data: {},
      async: true,
      success: reply => {},
      error: reply => {}
    },
    dataArray = [];

  options = Object.assign(params, options);
  options.type = options.type.toUpperCase();
  if (!options.url) return;
  let xmlhttp = XMLHttpRequest ?
    new XMLHttpRequest() :
    new ActiveXObject("Microsoft.XMLHTTP");

  if (typeof options.data == "object")
    for (const key in options.data)
      dataArray.push(key + "=" + options.data[key]);
  else dataArray.push(options.data);

  if (options.type === "GET") {
    let furl =
      dataArray.length == 0 ?
      options.url :
      options.url + "?" + dataArray.join("&");
    xmlhttp.open(options.type, furl, options.async);
    xmlhttp.send();
  } else if (options.type === "POST") {
    xmlhttp.open(options.type, options.url, options.async);
    xmlhttp.setRequestHeader(
      "Content-Type",
      "application/x-www-form-urlencoded"
    );
    xmlhttp.send(dataArray.join("&"));
  } else {
    return;
  }

  xmlhttp.onreadystatechange = function () {
    if (
      xmlhttp.readyState === 4 &&
      (xmlhttp.status === 200 || xmlhttp.status === 304)
    ) {
      options.success(xmlhttp.responseText);
    } else {
      options.error(xmlhttp.responseText);
    }
  };
};

/*
 * 函数: e
 * 参数: selector
 * 返回值: Object | Array | undefined
 */
const e = selector => {
  let objects = document.querySelectorAll(selector);
  if (objects.length <= 1) objects = objects[0];
  return objects;
};

/*
 * 函数: tablegen
 * 参数: Array,Array
 * 返回值: Object
 */
const tablegen = (array, titles) => {
  let $table = document.createElement("table"),
    $titleTr = document.createElement("tr");
  if (typeof array !== "object" || array.length === 0) return $table;
  if (typeof titles !== "object") {
    for (let t in array[0]) {
      let newTh = document.createElement("th");
      newTh.innerHTML = t;
      $titleTr.append(newTh);
    }
  } else {
    for (let t of titles) {
      let newTh = document.createElement("th");
      newTh.innerHTML = t;
      $titleTr.append(newTh);
    }
  }
  $table.append($titleTr);
  for (let obj of array) {
    let newTr = document.createElement("tr");
    for (let index in obj) {
      let newTd = document.createElement("td");
      newTd.innerHTML = obj[index];
      newTr.append(newTd);
    }
    $table.append(newTr);
  }
  return $table;
};

/*
 * 函数: datalistgen
 * 参数: String, Array
 * 返回值: Object
 */
const datalistgen = (listname, array) => {
  let $datalist = document.createElement('datalist');
  $datalist.id = listname;
  for (let data of array) {
    let $option = document.createElement('option');
    for (name in data) {
      if (/No/.test(name)) {
        $option.value = data[name];
      } else if (/name/.test(name)) {
        $option.innerText = data[name];
      }
    }
    $datalist.append($option);
  }
  return $datalist;
};

/*
 *  Cookie
 * */
class Cookie {
  constructor() {
    this.cpath = "/";
    this.keep = 0;
  }

  setCookie(name, value) {
    this.cname = name;
    this.cvalue = encodeURI(value);
    return this;
  }

  setTime(time) {
    time = typeof time === "undefined" ? 0 : time;
    this.keep = new Date();
    this.keep.setTime(this.keep.getTime() + time);
    return this;
  }

  setPath(path) {
    this.cpath = path;
    return this;
  }

  write() {
    let newCookie;
    if (this.keep === 0) {
      newCookie = this.cname + "=" + this.cvalue + ";";
    } else {
      newCookie = this.cname + "=" + this.cvalue + ";" + "expires=" + this.keep;
    }
    newCookie += ";path=" + this.cpath;
    document.cookie = newCookie;
  }

  find(name) {
    let decodedCookie = decodeURIComponent(document.cookie);
    let cookies = decodedCookie.split(";");

    for (let cookie of cookies) {
      let data = cookie.trim().split("=");
      if (name === data[0]) {
        return data[1];
      }
    }
    return "";
  }

  getCookies() {
    return document.cookie;
  }
}

//检测登录
(win => {
  win.cookie = new Cookie();
  let nowLocale = win.location.href;

  //没有登录就送去登录页面
  if (cookie.find("user") === "") {

    if (!/erp\/login/.test(nowLocale)) {
      win.location.href = '/erp/login';
    }
  } else {
    if (!/erp/.test(nowLocale)) win.location.href = '/erp';
  }
})(window);